package com.fc.control;

import java.sql.SQLException;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;

import com.fc.control.UserControlPage;
import com.fc.util.Pagination;
import com.fc.db.DBConnect;
import com.fc.model.Admin;

public class AdminControl {
	
	public static Pagination pageInstance = null;
	
	//add admin method
	public static Boolean addAdmin(Admin admin){
		DBConnect dbc = new DBConnect();
		String sql = "insert into admin (role,sex,phone,username,password,idcard) value(?,?,?,?,?,?)";
		dbc.prepareStatement(sql);
		try{
			dbc.setInt(1,admin.getRole());
			dbc.setInt(2,admin.getSex());
			dbc.setString(3, admin.getPhone());
			dbc.setString(4,admin.getUsername());
			dbc.setString(5,admin.getPassword());
			dbc.setString(6,admin.getIdcard());
			dbc.executeUpdate();
			return true;
		}catch(SQLException e){
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
	}
	//remove admin method
	public static Boolean deleteAdmin(int id){
		DBConnect dbc = new DBConnect();
		String sql = "delete from admin where id=?";
		dbc.prepareStatement(sql);
		try{
			dbc.setInt(1,id);
			dbc.executeUpdate();
			return true;
		}catch(SQLException e){
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
	}
	//update admin method
	@SuppressWarnings("finally")
	public static Boolean updateAdmin(Admin admin,String name){
		DBConnect dbc = new DBConnect();
		String sql = "update admin set password= ?,phone=? where username=?";
		dbc.prepareStatement(sql);
		try{
			dbc.setString(1, admin.getPassword());
			dbc.setString(2,admin.getPhone());
			dbc.setString(3, name);
			dbc.executeUpdate();
			return true;
		}catch(SQLException e){
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
			//return false;
		}
	}
	//get admin information by id
	public static Admin getAdminInfo(int id){
		DBConnect dbc = new DBConnect();
		String sql = "select * from admin where id=?";
		dbc.prepareStatement(sql);
		try{
			Admin admin = new Admin();
			dbc.setInt(1,id);
			ResultSet rs = dbc.executeQuery();
		if(rs.next())
		{
			admin.setRole(rs.getInt("role"));
			admin.setSex(rs.getInt("sex"));
			admin.setUsername(rs.getString("username"));
			admin.setPassword(rs.getString("password"));
			admin.setIdcard(rs.getString("idcard"));
			admin.setPhone(rs.getString("phone"));
			return admin;
		}
	}catch(SQLException e){
		e.printStackTrace();
		return null;
	}finally{
		dbc.close();
	}
	return null;
	}
	
	//get admin id by username and password
	public static int getAdminId(String username,String password){
		DBConnect dbc = new DBConnect();
		String sql = "select * from admin where username=? and password=?";
		dbc.prepareStatement(sql);
	try{
		dbc.setString(1,username);
		dbc.setString(2,password);
		ResultSet rs = dbc.executeQuery();
		if(rs.next())
			return rs.getInt("id");
	}catch(SQLException e){
		e.printStackTrace();
	}finally{
		dbc.close();
		return 0;
	}
  }
	//judge admin
	public boolean isAdmin(String username, String password) {
		DBConnect dbc = new DBConnect();
		String sql = "SELECT * FROM admin WHERE username = ? AND password = ?";
		dbc.prepareStatement(sql);
		try {
			dbc.setString(1, username);
			dbc.setString(2, password);
			ResultSet rs = dbc.executeQuery();
			if (rs.next()){
				return true;
			}
			else{
				System.out.print("ssss");
			
				return false;}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbc.close();
		}
		return false;
	}
	//judge username
	public static boolean isUsername(String username){
		DBConnect dbc = new DBConnect();
		String sql = "SELECT * FROM admin WHERE username = ?";
		dbc.prepareStatement(sql);
		try {
			dbc.setString(1, username);
			ResultSet rs = dbc.executeQuery();
			if (rs.next()){
				return true;
			}
			else
				return false;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbc.close();
		}
		return false;
	}

	//pagination method
	public Collection getAdmin(int page,int rowsPerPage)throws SQLException,Exception{
		String sql = "select * from admin";
		pageInstance = new UserControlPage(sql,rowsPerPage);
		try{
			return pageInstance.getPage(page);
		}catch(SQLException e){
			e.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	public Collection getAdmin(int page, int rowsPerPage,String username) throws SQLException, Exception
	{
		String sql = "SELECT * FROM admin where username = '"+username+"'";
		pageInstance = new UserControlPage(sql, rowsPerPage);
		try {
			return pageInstance.getPage(page);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}


class UserControlPage extends Pagination{
	public UserControlPage(String sql,int rowsPerPage){
		super(sql,rowsPerPage,1);
	}
	@Override
	protected Collection<Admin> packResultSet(ResultSet rs) throws SQLException {
		Collection<Admin> adminList = new ArrayList<Admin>();
		while (rs.next()) {
			Admin admin=new Admin();
			admin.setId(rs.getInt("id"));
			admin.setUsername(rs.getString("username"));
			admin.setPassword(rs.getString("password"));
			admin.setRole(rs.getInt("role"));
			admin.setSex(rs.getInt("sex"));
			admin.setPhone(rs.getString("phone"));
			adminList.add(admin);
			admin = null;
		}
		return adminList;
	}
	
}
